package cn.eonshine.cuba.lsms.modular.biz.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.biz.entity.InventoryData;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.biz.entity.SRealOildepotStock;
import cn.eonshine.cuba.lsms.modular.biz.entity.SRealOildepotStockDay;
import cn.eonshine.cuba.lsms.modular.biz.service.IInfRealTimeDataOffService;
import cn.eonshine.cuba.lsms.modular.biz.service.IInventoryDataService;
import cn.eonshine.cuba.lsms.modular.biz.service.ISRealOildepotStockDayService;
import cn.eonshine.cuba.lsms.modular.biz.service.ISRealOildepotStockService;

@Service
public class InventoryDataService extends CommonService<InventoryData> implements IInventoryDataService {
	@Autowired
	private ISRealOildepotStockService sRealOildepotStockService;
	
	@Autowired
	private IInfRealTimeDataOffService infRealTimeDataOffService;
	
	@Autowired
	private ISRealOildepotStockDayService sRealOildepotStockDayService;
	
	private final String listUrl = "/inventoryData/list";
	private final String updateUrl = "/inventoryData/update";
	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.eonshine.cuba.inf.modular.lsms.basic.service.IInventoryDataService#
	 * requestInventoryData()
	 */
	@Override
	public void requestInventoryData() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.INVENTORY_NUM);
	}
	
	@Override
	protected Object dataHandle(InventoryData inventoryData) throws Exception {
		boolean isClosed=false;
		if(inventoryData.getRptDate() != null) {
			isClosed = infRealTimeDataOffService.isClosed(inventoryData.getRptDate());
		}else {
			// 没有业务日期的数据，不接收此数据。
//			throw new Exception("");
			return null;
		}
		// 数据关闭，不再接收此数据。
		if(isClosed) {
			return null;
		}
		
		
		QueryWrapper<SRealOildepotStock> entity = new QueryWrapper<SRealOildepotStock>();
//		entity.where("OIL_DEPOT={0} and OILCAN_ID={1} and OIL_ID={2} and DATA_DATE={3} ", inventoryData.getOrgId(), inventoryData.getOilcanId(), inventoryData.getOilId(), inventoryData.getRptDate());
		entity.lambda().eq(SRealOildepotStock::getOilDepot, inventoryData.getOrgId());
		entity.lambda().eq(SRealOildepotStock::getOilcanId, inventoryData.getOilcanId());
//		entity.lambda().eq(SRealOildepotStock::getOilId, inventoryData.getOilId());
		entity.lambda().eq(SRealOildepotStock::getDataDate, inventoryData.getRptDate());
		SRealOildepotStock info = sRealOildepotStockService.getOne(entity);

		boolean isInsert = false;
		if (info == null) {
			info = new SRealOildepotStock();
			isInsert = true;
		}

		info.setOilDepot(inventoryData.getOrgId());
		info.setOilcanId(inventoryData.getOilcanId());
		info.setOilId(inventoryData.getOilId());
		info.setQuantity(inventoryData.getQuantity());
		info.setDataDate(inventoryData.getRptDate());
		if (StringUtils.isNotBlank(inventoryData.getRemarks())) {
			info.setRemarks(inventoryData.getRemarks());
		}
		info.setEmptyTankQuantity(inventoryData.getEmptyTankQuantity());
		info.setThirdPartyQuantity(inventoryData.getThirdPartyQuantity());

		Date nowTime = DateUtils.getCubaDate(Calendar.getInstance().getTime());
		if(inventoryData.getCreateTime() != null) {
			if(info.getCreateDate() == null) {
				info.setCreateDate(inventoryData.getCreateTime());
			}
		}else if(isInsert){
			info.setCreateDate(nowTime);
		}
		if(inventoryData.getDataInputDatetime() != null) {
			info.setUpdateDate(inventoryData.getDataInputDatetime());
		}else {
			info.setUpdateDate(nowTime);
		}
		
		if (isInsert) {
			sRealOildepotStockService.save(info);
		} else {
			sRealOildepotStockService.updateById(info);
		}
		
		handleTrancDay(info);
		return info;
	}
	
	@Override
	protected List<InventoryData> feadBackDataHandle(Object obj, InventoryData inventoryData, List<InventoryData> feadBackDataLists) throws Exception {
		SRealOildepotStock info = (SRealOildepotStock) obj;
		// 返回进行更新的数据
		InventoryData tmpBack = new InventoryData();
		tmpBack.setOrgId(inventoryData.getDmzOrgId());
		tmpBack.setOilcanId(inventoryData.getDmzOilCanId());
		tmpBack.setOilId(inventoryData.getDmzOilId());
		tmpBack.setDataInputDatetime(inventoryData.getDataInputDatetime());
		tmpBack.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		tmpBack.setId(info.getId());
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
	
	/**
	 * 
	 * @Title: handleTrancDay   
	 * @Description: Report Of Real Oil Depot  
	 * by Alan 2019-04-10 10:58:49
	 * @param info      
	 * @return  void
	 */
	private void handleTrancDay(SRealOildepotStock info) {
		final BeanCopier copier = BeanCopier.create(SRealOildepotStock.class, SRealOildepotStockDay.class, false);
		SRealOildepotStockDay day = new SRealOildepotStockDay();
		copier.copy(info, day, null);
		day.setId(null);
		day.setbDate(DateUtils.truncate(info.getDataDate(), Calendar.DATE));
		
		QueryWrapper<SRealOildepotStockDay> entity = new QueryWrapper<SRealOildepotStockDay>();
//		entity.where("b_date={0} and oil_depot={1} and oilcan_id={2} ", day.getbDate(), day.getOilDepot(), day.getOilcanId());
		entity.lambda().eq(SRealOildepotStockDay::getbDate, day.getbDate());
		entity.lambda().eq(SRealOildepotStockDay::getOilDepot, day.getOilDepot());
		entity.lambda().eq(SRealOildepotStockDay::getOilcanId, day.getOilcanId());
		SRealOildepotStockDay dbDay = sRealOildepotStockDayService.getOne(entity);
		if(dbDay != null) {
			sRealOildepotStockDayService.update(day, entity);
		}else {
			sRealOildepotStockDayService.save(day);
		}
		
	}
}
